package com.hbpu.encap.houserent.view;

import com.hbpu.encap.houserent.domain.House;
import com.hbpu.encap.houserent.service.HouseService;
import com.hbpu.encap.houserent.utils.Utility;

public class HouseView {
    private HouseService houseService = new HouseService();
    public void mainMenu() {
        boolean loop = true;
        char choice;
        do {
            System.out.println("----------房屋出租系统----------");
            System.out.println("\t\t1 新 增 房 源");
            System.out.println("\t\t2 查 找 房 屋");
            System.out.println("\t\t3 删 除 房 屋");
            System.out.println("\t\t4 修 改 房 屋 信 息");
            System.out.println("\t\t5 房 屋 列 表");
            System.out.println("\t\t6 退 出");
            System.out.print("请输入你的操作序号：");
            choice = Utility.readMenuSelection();
            switch (choice) {
                case '1':
                    addHouse();
                    break;
                case '2':
                    searchHouse();
                    break;
                case '3':
                    deleteHouse();
                    break;
                case '4':
                    updateHouse();
                    break;
                case '5':
                    listHouse();
                    break;
                case '6':
                    System.out.println("是否退出系统？（y/n）");
                    char confirm = Utility.readChar();
                    if (confirm == 'y') {
                        System.out.println("程序退出~");
                        loop = false;
                    } else if (confirm == 'n') {
                        break;
                    }
            }
        } while (loop);

    }

    public void listHouse() {
        System.out.println("----------房 屋 列 表----------");
        System.out.println("id\t\t姓名\t\t电话\t\t地址\t\t租金\t\t状态");
        House[] houses = houseService.list();
        for (int i = 0; i < houses.length; i++) {
            if (houses[i] == null) {
                break;
            } else {
                System.out.println(houses[i]);
            }
        }
    }

    public void addHouse() {
        System.out.println("请输入添加信息：");
        System.out.print("姓名：");
        String name = Utility.readString(6);
        System.out.print("电话：");
        String tel = Utility.readString(11);
        System.out.print("地址：");
        String address = Utility.readString(10);
        System.out.print("租金：");
        int rent = Utility.readInt(1200);
        System.out.print("状态：");
        String state = Utility.readString(3);
        House newHouse = new House(0, name, tel, address, rent, state);
        houseService.add(newHouse);
    }

    public void deleteHouse() {
        listHouse();
        System.out.print("请输入你要删除的房屋信息的id：");
        int deleteID = Utility.readInt();
        houseService.delete(deleteID);
    }

    public void searchHouse() {
        System.out.print("请输入要查找的id：");
        int searchID = Utility.readInt();
        House house = houseService.search(searchID);
        if (house != null) {
            System.out.println("id\t\t姓名\t\t电话\t\t地址\t\t租金\t\t状态");
            System.out.println(house);
        } else {
            System.out.println("未查到该id对应的房屋信息！");
        }
    }

    public void updateHouse() {
        System.out.print("请输入要修改的房屋信息的ID：");
        int updateID = Utility.readInt();
        House house = houseService.search(updateID);
        if (houseService.update(house)) {
            System.out.println("修改成功！");
        } else {
            System.out.println("修改失败！");
        }
    }
}

